package com.wenjh.jdmzt.android.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.wenjh.jdmzt.AndroidKit;
import com.wenjh.jdmzt.R;
import com.wenjh.jdmzt.android.activity.MainActivity;
import com.wenjh.jdmzt.android.synctask.LoadImageUtil;
import com.wenjh.jdmzt.android.view.HandyListView;
import com.wenjh.jdmzt.service.model.Image;
import com.wenjh.jdmzt.util.Log4Android;

public class ImageGridAdapter extends BaseGridViewAdapter<Image> implements OnClickListener {
	private Log4Android log = new Log4Android(this);
	private static final int LineCount = 3;
	HandyListView listView = null;
	MainActivity mainActivity = null;
	
	public ImageGridAdapter(Context context, List<Image> list, int linewidth, HandyListView listView) {
		super(context, list, linewidth, AndroidKit.getPixels(1.5f));
		this.listView = listView;
		this.mainActivity = (MainActivity) context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView == null) {
			LinearLayout layout = (LinearLayout) inflate(R.layout.listitem_gridline);
			convertView = layout;
			
			for(int i=0; i<LineCount; i++) {
				View view = inflate(R.layout.listitem_griditem);
				ViewHolder holder = new ViewHolder();
				view.setTag(holder);
				holder.avatorView = (ImageView) view.findViewById(R.id.imageview);
				view.setOnClickListener(this);
				layout.addView(view);
				
				LayoutParams layoutParams = holder.avatorView.getLayoutParams();
				layoutParams.height = itemSize;
				layoutParams.width = itemSize;
				holder.avatorView.setLayoutParams(layoutParams);
				holder.avatorView.setPadding(leftPadding, topPadding, rightPadding, bottomPadding);
				view.setOnClickListener(clickListener);
			}
		}
		
		if(position == 0) {
			convertView.setPadding(0, 12, 0, 0);
		} else {
			convertView.setPadding(0, 0, 0, 0);
		}
		
		int startPosition = position * getLineCount();
		int endPosition = startPosition + getLineCount();
		int itemIndex = 0;
		
		for(int i = startPosition; i< endPosition; i++) {
			View v = ((ViewGroup)convertView).getChildAt(itemIndex++);
			if(i > items.size()-1) {
				v.setVisibility(View.INVISIBLE);
				continue;
			}
			
			ViewHolder holder = (ViewHolder) v.getTag();
			v.setVisibility(View.VISIBLE);
			v.setTag(R.id.tag_item_position, i);
			LoadImageUtil.loadImage(getItem(i), holder.avatorView, listView, true, false);
		}
		
		return convertView;
	}

	private final class ViewHolder {
		ImageView avatorView;
	}

	@Override
	public int getLineCount() {
		return LineCount;
	}
	
	@Override
	public int getCount() {
		float f = (float)(getItems().size()) / (float)getLineCount();
		return (int)(f+0.9);
	}

	@Override
	public View getItemView(int position, View convertView, ViewGroup parent) {
		return null;
	}

	@Override
	public void onClick(View v) {
		
	}
	
	private OnClickListener clickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if(v.getTag(R.id.tag_item_position) != null) {
				int pos = (Integer) v.getTag(R.id.tag_item_position);
				mainActivity.onImageClick(pos);
			}
		}
	};
}
